package com.eims.sls_app.view;

import com.eims.sls_app.utils.AppTools;
import com.eims.sls_app.utils.ImageManager;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import bls.juejin.cn.R;

/**
 * 自定义提示框
 **/
@SuppressLint("UseSparseArrays")
public class ConfirmUpdateDialog extends Dialog implements OnClickListener {
	private final static String TAG = "ConfirmDialog";

	private Context context;
	private TextView tv_title;// 提示
	private TextView dl_tv_content;// 内容
	private Button dl_btn_cancel;// 取消
	private Button dl_btn_confirm;// 确定
	private ImageView ivBg;//显示提示图片

	private String UrLImg = "";
	Bitmap bitmap;

	public ConfirmUpdateDialog(Context context) {
		super(context);
	}

	public ConfirmUpdateDialog(Context context, boolean cancelable,
							   OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
	}

	public ConfirmUpdateDialog(Context context, int theme, String url) {
		super(context, theme);
		this.UrLImg = url;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_confirm_update);
		findView();
		setListner();
	}

	/**
	 * 设置确认按钮的文字
	 */
	public void setDialogConfirmText(String confirmText) {
		dl_btn_confirm.setText(confirmText);
	}

	/**
	 * 设置取消按钮的文字
	 */
	public void setDialogCancelText(String cancelText) {
		dl_btn_cancel.setText(cancelText);
	}

	/**
	 * 设置标题
	 *
	 * @param title
	 */
	public void setDialogTitle(String title) {
		tv_title.setText(title);
	}

	/**
	 * 设置标题
	 *
	 * @param -title
	 */
	public void setDialogTitlebg(String url) {
		UrLImg = url;


	}

	public void setBackIm(final String url) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				// 写子线程中的操作
				bitmap = AppTools.getHttpBitmap(UrLImg);



				handler.sendEmptyMessage(100);


			}
		}).start();
	}


	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case 100:

					// 写子线程中的操作
					ivBg.setImageBitmap(bitmap);


					break;

				default:
					break;
			}
		}

		;
	};


	/**
	 * 设置内容
	 *
	 * @param content
	 */
	public void setDialogContent(String content) {
		Log.i(TAG, content);
		Log.i(TAG, dl_tv_content + "");
		dl_tv_content.setText(content);
	}

	public void findView() {
//		tv_title = (TextView) findViewById(R.id.tv_title);
//		dl_tv_content = (TextView) findViewById(R.id.dl_tv_content);
		ivBg = (ImageView) findViewById(R.id.dialog_iv_bg);
		dl_btn_cancel = (Button) findViewById(R.id.dl_btn_cancel);
		dl_btn_confirm = (Button) findViewById(R.id.dl_btn_confirm);

		setBackIm(UrLImg);


	}

	/**
	 * 绑定监听
	 **/
	private void setListner() {
		dl_btn_cancel.setOnClickListener(this);
		dl_btn_confirm.setOnClickListener(this);
	}

	/**
	 * 点击监听
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.dl_btn_cancel:
				if (null != this.listener) {
					listener.getResult(0);
				}
				ConfirmUpdateDialog.this.dismiss();
				break;
			case R.id.dl_btn_confirm:
				if (null != this.listener) {
					listener.getResult(1);
				}
				ConfirmUpdateDialog.this.dismiss();
				break;
		}
	}

	private DialogResultListener listener;

	public void setDialogResultListener(DialogResultListener listener) {
		this.listener = listener;
	}

	public interface DialogResultListener {
		/**
		 * 获取结果的方法
		 *
		 * @param resultCode 0.取消 1.确定
		 */
		void getResult(int resultCode);
	}

}
